<?php
/*
Plugin Name: Nino Contact Pro
Plugin URI: http://www.ninotheme.com
Description: Nino Contact Pro
Author: ninotheme.com
Version: 1.0
Author URI: http://www.ninotheme.com
*/

session_start();
global $nino_contact_pro_db_version;
$nino_contact_pro_plugin_version = '1.0';
$nino_contact_pro_db_version = '0.1.0';

/********************
 * Global constants
 ********************/
define( 'NINO_CONTACT_PRO_URL', plugin_dir_url( __FILE__ ) );

include('includes/admin-page.php');
include('includes/settings/basic-elements-settings.php');
include('includes/settings/basic-properties-settings.php');
include('includes/contact-form-pro-ajax.php');
include('includes/contact-form-pro-widget.php');
include('includes/contact-form-pro-render.php');
include('includes/contact-form-pro-db.php');
include('includes/admin/contact-render-elements.php');
include('includes/admin/contact-render-elements-settings.php');

register_activation_hook(__FILE__, 'nino_contact_pro_active');
register_uninstall_hook(__FILE__, 'nino_contact_pro_uninstall');

add_action( 'wpmu_new_blog', 'nino_contact_pro_new_blog', 10, 6);
add_action( 'delete_blog', 'nino_contact_pro_delete_blog', 10, 2); 

function nino_contact_pro_active($networkwide) {
	global $wpdb;
	global $nino_contact_pro_db_version;
	update_option("nino_contact_pro_db_version", $nino_contact_pro_db_version);
	
	if (function_exists('is_multisite') && is_multisite()) {
        // check if it is a network activation - if so, run the activation function for each blog id
        if ($networkwide) {
            $old_blog = $wpdb->blogid;
            // Get all blog ids
            $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
            foreach ($blogids as $blog_id) {
                switch_to_blog($blog_id);
                nino_contact_pro_create_metadata_table();
            }
            switch_to_blog($old_blog);
            return;
        }  
    } else {
    	nino_contact_pro_create_metadata_table();
    }
    
}

function nino_contact_pro_uninstall($networkwide) {
	global $wpdb;
	delete_option("hfr_db_version");
	
	if (function_exists('is_multisite') && is_multisite()) {
        // check if it is a network activation - if so, run the activation function for each blog id
        if ($networkwide) {
            $old_blog = $wpdb->blogid;
            // Get all blog ids
            $blogids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");
            foreach ($blogids as $blog_id) {
                switch_to_blog($blog_id);
                nino_contact_pro_remove_metadata_table();
            }
            switch_to_blog($old_blog);
            return;
        }  
    } else {
    	nino_contact_pro_remove_metadata_table();
    }
    
}   
 
function nino_contact_pro_new_blog($blog_id, $user_id, $domain, $path, $site_id, $meta ) {
    global $wpdb;
 
    if (is_plugin_active_for_network('nino-contact-pro/nino-contact-pro.php')) {
        $old_blog = $wpdb->blogid;
        switch_to_blog($blog_id);
        nino_contact_pro_create_metadata_table();
        switch_to_blog($old_blog);
    }
}

function nino_contact_pro_delete_blog($blog_id) {
	global $wpdb;
 
    if (is_plugin_active_for_network('nino-contact-pro/nino-contact-pro.php')) {
        $old_blog = $wpdb->blogid;
        switch_to_blog($blog_id);
        nino_contact_pro_remove_metadata_table();
        switch_to_blog($old_blog);
    }
}